/*
 *  ┌─────────────────────────────────────────────────────────────┐
 *  │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│
 *  ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││
 *  │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│
 *  ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││
 *  │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│
 *  ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││
 *  │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│
 *  ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││
 *  │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│
 *  │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │
 *  │      └───┴─────┴───────────────────────┴─────┴───┘          │
 *  └─────────────────────────────────────────────────────────────┘
 *
 * @Author: xiaoyang
 * @Date: 2021-04-27 14:49:42
 * @LastEditors: xiaoyang
 * @LastEditTime: 2021-04-30 18:05:32
 * @FilePath: \ruoyi-uif:\program_code\loan\main\js\utils.js
 * @description: No description
 */

/**
 * @description:用于获取url参数:
 * @param {*} variable
 * @return {*}
 */
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

/**
 * @description:  对输入的数字进行处理
 * @param {*} value
 * @return {*}
 */
function numberFilter(key, value, event, _this) {
  value = value[0] === '.' ? '0' + value : value;
  value = value.length > 6 ? value.substr(0, 6) : value;
  _this[key] = value;
}

/**
 * @description: 计算等额本息
 * @param {*}
 * @return {*}
 * @var loanTotalPrice 贷款金额
 * @var q 月利率
 * @var tl 总月数
 */
function computerLoan(params, dueType) {
  // 等额本息
  const { loanTotalPrice, loanTimeLength, oldLoanLPR } = params;
  let tl = loanTimeLength * 12; //分期数
  let q = oldLoanLPR / 100 / 12; //月利率
  let t = Math.pow(1 + q, tl) / (Math.pow(1 + q, tl) - 1); //1.2268
  let em = loanTotalPrice * 10000 * q * t; //每月固定月供总额

  let list = [];
  for (let i = 0; i < tl; i++) {
    list.push({
      month: i + 1,
      everyMonthPrice: em,
      everyMonthInterest: (loanTotalPrice * 10000 - getTotalNumber(list)) * q,
      everyMonthPrincipal: em - (loanTotalPrice * 10000 - getTotalNumber(list)) * q,
      residue: em * tl - em * (i + 1),
    });
  }

  //  等额本金
  let d_princial = (loanTotalPrice * 10000) / tl; //2778 月供本金
  let d_interest = loanTotalPrice * 10000 * q; // 月供利息  贷款本金- 已归还本金累计额
  let d_residue = ((loanTotalPrice * 10000) / tl) * q; //固定值 每月利息递减
  let d_toatl_price = loanTotalPrice * 10000 + sum(tl, d_interest, d_residue); //还款总和
  let min = d_interest - (tl - 1) * d_residue;

  console.log(d_interest);
  console.log(min);
  // console.log(d_residue);
  // console.log(sum(tl, d_interest, d_residue));

  let arr = [
    {
      month: 1,
      everyMonthPrice: d_princial + loanTotalPrice * 10000 * q, //月供总额
      everyMonthInterest: d_interest, //每月应还利息
      everyMonthPrincipal: d_princial, //月供本金 固定值
      residue: d_toatl_price - (d_princial + loanTotalPrice * 10000 * q),
    },
  ];

  for (let i = 1; i < tl; i++) {
    let t1 = (loanTotalPrice * 10000) / tl + (loanTotalPrice * 10000 - i * d_princial) * q;
    arr.push({
      month: i + 1,
      everyMonthPrice: (loanTotalPrice * 10000) / tl + (loanTotalPrice * 10000 - i * d_princial) * q,
      everyMonthInterest: (loanTotalPrice * 10000 - i * d_princial) * q,
      everyMonthPrincipal: d_princial,
      residue: arr[i - 1].residue - t1,
    });
  }

  let data = [
    // id 为1 代表等额本息
    // id 为2 代表等额本金
    {
      id: 1,
      list: list,
      loanTotalPrice: loanTotalPrice * 10000, //贷款总额,
      interestTotal: em * tl - loanTotalPrice * 10000, //利息总和，
      totalPrice: em * tl, //总还款金额 利息+本金,
      tl,
    },
    {
      id: 2,
      list: arr,
      loanTotalPrice: loanTotalPrice * 10000, //贷款总额,
      interestTotal: d_toatl_price - loanTotalPrice * 10000, //利息总和，
      totalPrice: d_toatl_price, //总还款金额 利息+本金,
      tl,
    },
  ];

  if (dueType === 3 && dueType > 0) {
    return data;
  } else {
    localStorage.setItem('loan', JSON.stringify(data));
  }
}

/**
 * @description:
 * @param {*}
 * @return {*}
 */

function getTotalNumber(list) {
  let total = 0;
  for (let j = 0; j < list.length; j++) {
    total += list[j].everyMonthPrincipal;
  }
  return total;
}

/**
 * @description:获取总利息
 * @param {*} tl 期数
 * @param {*} d 利息
 * @param {*} r 差值
 * @return {*}
 */
function sum(tl, d, r) {
  let total = d;
  for (let i = 1; i < tl; i++) {
    d = d - r;
    total += d;
  }
  return total;
}

/**
 * @description:
 * @param {*} list
 * @param {*} t1
 * @return {*}
 */
function getTotal(list, t1) {
  let total = 0;
  for (let j = 0; j < list.length; j++) {
    total += list[j].everyMonthPrice + t1;
  }
  return total;
}
